package doughnut.xc.Demo;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;

/**
 * 使用jacob将 word转为pdf
 */
public class JacobDemo {
    public static void main(String[] args) {

        String source = "F:\\可视化\\可视化\\资料(3)\\张三_合同.docx";
        String target = "F:\\可视化\\可视化\\资料(3)\\张三_合同.pdf";
        // 调用window程序
        ActiveXComponent axc = null;
        try {
            axc = new ActiveXComponent("Word.Application");
            // 调用的时候不显示窗口
            axc.setProperty("Visible", false);
            // 获得所有打开的文档
            Dispatch docs = axc.getProperty("Documents").toDispatch();
            Dispatch doc = Dispatch.call(docs, "Open", source).toDispatch();
            // 另存为，将文档保存为pdf，其中Word保存为pdf的格式宏的值是17
            Dispatch.call(doc, "SaveAs", target, 17);
            Dispatch.call(doc, "Close");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (axc != null)
              axc.invoke("Quit", 0);
        }
    }
}
